package com.homwee.factory;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.os.Bundle;

import java.util.concurrent.TimeUnit;

public class FactoryLightSensorActivity extends Activity {
    private SensorManager sensorManager;
    private RelativeLayout mRootLayout,mainLayout;
    EditText mEditText =null;
    TextView barCodeTextViewValue;
    TextView barCodeTextViewPresentationText;
    TextView barCodeTextViewPid ;
    String  factoryBarCodeStr ="";
    public static FactoryLightSensorActivity instance=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance=this;
        LayoutInflater vi = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootLayout = (RelativeLayout)vi.inflate(R.layout.factory_menu_bar_code_view,null);
        setContentView(mRootLayout);
        initView();

        //获得感应器服务
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //获得光线感应器
        Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        //注册监听器
        sensorManager.registerListener(listener, sensor, SensorManager. SENSOR_DELAY_UI);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d("tv","keyCode "+keyCode);
        switch (keyCode) {
            case KeyEvent.KEYCODE_TV_POWER:
            case KeyEvent.KEYCODE_SETTINGS:
            case KeyEvent.KEYCODE_MENU:
                this.finish();
                Log.v("KEYCODE_MENU","KEYCODE_MENU="+KeyEvent.KEYCODE_MENU);
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);

    }


    private void initView() {
        barCodeTextViewValue = (TextView)findViewById(R.id.factory_bar_code_view_value);
        barCodeTextViewValue.setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
        barCodeTextViewPresentationText = (TextView)findViewById(R.id.factory_bar_code_view_presentation_text);
        barCodeTextViewPresentationText.setTextColor(Color.WHITE);;
        barCodeTextViewPresentationText.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);
        barCodeTextViewPid = (TextView)findViewById(R.id.factory_bar_code_view_pid);
        mainLayout =(RelativeLayout)mRootLayout.findViewById(R.id.common_bar_code_view);
        barCodeTextViewPid.setText("Check Light Sensor");
        mainLayout.setBackgroundResource(R.drawable.factory_toast);
        mainLayout.getBackground().setAlpha(200);

        barCodeTextViewPresentationText.setText("ERROR");
        barCodeTextViewPresentationText.setTextColor(Color.RED);;
    }


    //Activity被销毁
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销监听器
        if (sensorManager != null) {
            sensorManager.unregisterListener(listener);
        }
    }

    //感应器事件监听器
    private SensorEventListener listener = new SensorEventListener() {
        //当感应器精度发生变化
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
        //当传感器监测到的数值发生变化时
        @Override
        public void onSensorChanged(SensorEvent event) {
            float light = event.values[0] * 0.0263f;
            long lux = (long) light;
            barCodeTextViewValue.setText("Current Lux : " + lux );
            if(lux>0){
                barCodeTextViewPresentationText.setText("OK");
                barCodeTextViewPresentationText.setTextColor(Color.GREEN);;
            }else{
                barCodeTextViewPresentationText.setText("ERROR");
                barCodeTextViewPresentationText.setTextColor(Color.RED);;
            }
        }
    };
}
